<template>
  <div class="videoPromotionstyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <!-- 表单 -->
    <el-form label-width="80px" :model="datas" size="small">
      <!-- 选择样式 -->
      <el-form-item label="选择样式" class="lef">
        <p style="color: #000">{{ styleText }}</p>
      </el-form-item>
      <!-- 样式选择 -->
      <div class="vidCommodityType">
        <el-tooltip class="item" effect="dark" content="视频带链接" placement="bottom">
          <span
            class="iconfont icon-shipin"
            style="font-size: 21px"
            :class="datas.commodityType === 0 ? 'active' : ''"
            @click="datas.commodityType = 0"
          />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="视频+产品" placement="bottom">
          <span
            class="iconfont icon-commodity-xiangxiliebiao"
            style="font-size: 24px"
            :class="datas.commodityType === 1 ? 'active' : ''"
            @click="datas.commodityType = 1"
          />
        </el-tooltip>
      </div>
      <h5 style="color: #000; font-size: 14px">添加内容</h5>
      <p style="color: #969799; font-size: 12px; margin-top: 10px">
        拖动选中的导航可对其排序
      </p>
      <div v-show="datas.commodityType === 0">
        <!-- 图片广告 -->
        <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgBanner">
              <i class="el-icon-circle-close" @click="deleteimg(index)" />
              <!-- 图片和视频 -->
              <div class="imgAndVideo">
                <div class="imag">
                  <img src="../../../assets/images/add.png" @click="showUpload('first',index)" alt draggable="false" v-if="!item.vimgUrl" />
                  <img :src="item.vimgUrl" @click="showUpload('first',index)" alt draggable="false" v-else />
                </div>
                <div class="upvideo">
                  <el-button v-if="!item.videoUrl" size="small" type="primary" @click="uploadVideo(index)">
                    上传视频<i class="el-icon-upload el-icon--right"></i>
                  </el-button>
                  <div v-else>
                    <video width="90" height="90" v-if="item.videoUrl" controls="controls">
                      <source :src="item.videoUrl" type="video/mp4" />
                    </video>
                    <p @click="deleteupvideo(index)">删除</p>
                  </div>
                </div>
              </div>
              <!-- 标题和链接 -->
              <div class="imgText">
                <el-input v-model="item.title" placeholder="标题" size="mini" clearable />
                <div style="height: 5px" />
                <el-input v-model="item.desc" placeholder="描述" size="mini" clearable />
                <div style="height: 5px" />
                <!-- 运营标签 -->
                <el-input v-model="item.flag" placeholder="运营标签" size="mini" clearable />
                <div style="height: 5px" />
                <el-input v-model="item.btnText" placeholder="按钮文字" size="mini" clearable />
                <div style="height: 5px" />
                <el-select v-model="item.sectionCode" @change="changeSection(item)" clearable placeholder="请选择学段">
                  <el-option
                    v-for="item in sectionList"
                    :key="item.id"
                    :label="item.tagEnumName"
                    :value="item.id + '-' + item.tagId + '-' + item.tagEnumCode">
                  </el-option>
                </el-select>
                <div style="height: 5px" />
                <el-select v-model="item.subjectCode" clearable placeholder="请选择科目">
                  <el-option
                    v-for="item in subjectList"
                    :key="item.id"
                    :label="item.tagEnumName"
                    :value="item.tagId + '-' + item.tagEnumCode">
                  </el-option>
                </el-select>
                <div style="height: 5px" />
                <!-- 选择类型 -->
                <select-link
                  @selectThisLink="selectThisLink"
                  @clearThisLink="clearThisLink(index)"
                  :linktypeurl="item.linkurl"
                  :theIndex="index"
                />
                <el-form-item class="lef" label="VIP标识">
                  <el-radio-group v-model="item.isVipShow">
                    <el-radio :label="2">隐藏</el-radio>
                    <el-radio :label="1">显示</el-radio>
                  </el-radio-group>
                </el-form-item>
              </div>
            </section>
          </transition-group>
        </vuedraggable>
      </div>
      <div v-show="datas.commodityType === 1">
        <div v-for="(item,index) in datas.imageList" :key="index" style="position:relative; border:1px solid #dddddd; padding:10px;">
          <i class="el-icon-circle-close" style="position: absolute; top:0; right: 0" @click="deleteimg(index)" />
          <!-- 图片和视频 -->
          <div class="imgAndVideo1">
            <div class="imag">
              <span>上传视频缩略图</span>
              <img src="../../../assets/images/add.png" @click="showUpload('first',index)" alt draggable="false" v-if="!item.vimgUrl" />
              <img :src="item.vimgUrl" @click="showUpload('first', index)" alt draggable="false" v-else />
            </div>
            <div class="upvideo">
              <span>上传头部视频</span>
              <el-button v-if="!item.videoUrl" size="small" type="primary" @click="uploadVideo(index)">
                上传视频<i class="el-icon-upload el-icon--right"></i>
              </el-button>
              <div style="position:relative;" v-else>
                <video width="120" height="120" v-if="item.videoUrl" controls="controls">
                  <source :src="item.videoUrl" type="video/mp4" />
                </video>
                <p @click="deleteupvideo(index)">删除</p>
              </div>
            </div>
          </div>
          <vuedraggable v-model="item.list" v-bind="dragOptions">
            <transition-group>
              <section v-for="(ite, ind) in item.list" :key="ite + ind" class="imgBanner">
                <i class="el-icon-circle-close" @click="deleteListImg(index,ind)" />
                <!-- 图片 -->
                <div class="imag">
                  <img src="../../../assets/images/add.png" @click="showUpload('second', index, ind)" alt draggable="false" v-if="!ite.src"/>
                  <img :src="ite.src" @click="showUpload('second', index, ind)" alt="" draggable="false"/>
                </div>
                <!-- 标题和链接 -->
                <div class="imgText">
                  <el-input v-model="ite.text" placeholder="标题" size="mini" clearable/>
                  <div style="height: 5px" />
                  <el-input v-model="ite.desc" placeholder="描述" size="mini" clearable/>
                  <div style="height: 5px" />
                  <!-- 标题和链接 -->
                  <div class="imgTextChild">
                    <!-- 选择类型 -->
                    <select-link
                      @selectThisLink="selectThisLink"
                      @clearThisLink="clearThisLink(index, ind)"
                      :linktypeurl="ite.url"
                      :theIndex="ind"
                      :sectionIndex="index"
                    />
                  </div>
                  <div style="height: 5px" />
                  <!-- 场景 -->
                  <el-input v-model="ite.studyStatus" placeholder="场景" size="mini" clearable/>
                  <div style="height: 5px" />
                  <!-- 运营标签 -->
                  <el-select v-model="ite.flagTagsArr" clearable multiple filterable remote reserve-keyword allow-create default-first-option placeholder="输入运营标签,按回车添加" @change="changeValue($event,index,ind)">
                    <el-option v-for="it in flagTags" :key="it.value" :label="it.label" :value="it.label"></el-option>
                  </el-select>
                  <div style="height: 5px" />
                  <!-- 按钮文字 -->
                  <el-input v-model="ite.btnText" placeholder="按钮文字" size="mini" clearable/>
                  <el-form-item class="lef" label="VIP标识">
                    <el-radio-group v-model="ite.isVipShow">
                      <el-radio :label="2">隐藏</el-radio>
                      <el-radio :label="1">显示</el-radio>
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item class="lef" label="价格显示">
                    <el-radio-group v-model="ite.showPrice" @change="radioChange($event,index,ind)">
                      <el-radio :label="2">隐藏</el-radio>
                      <el-radio :label="1">显示</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </div>
              </section>
            </transition-group>
            <el-button class="uploadImg" style="margin-top:10px; width:53%" type="primary" plain @click="addVideoContent(index)">
              <i class="el-icon-plus" />点击添加内容
            </el-button>
          </vuedraggable>
        </div>
      </div>
      <!-- 添加内容 -->
      <el-button
        class="uploadImg"
        style="margin-top:15px; width:100%;"
        type="success"
        plain
        @click="addVideoList(datas.commodityType)">
        <i class="el-icon-plus" />点击添加内容
      </el-button>
    </el-form>
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
    <!-- 上传视频 -->
    <uploadVideo ref="uploadvideo" @uploadVideoSrc="uploadVideoSrc" />
  </div>
</template>
<script>
import vuedraggable from "vuedraggable"; // 拖拽组件
import uploadimg from "../../uploadImg"; // 图片上传
import SelectLink from "../../selectLink";
import uploadVideo from "../../uploadVideo"; // 视频上传
import { getFirstLevels, getChildrenList } from "@/api/basisMG";
export default {
  name: "VideoPromotionstyle",
  components: {
    vuedraggable,
    uploadimg,
    SelectLink,
    uploadVideo
  },
  computed: {
    styleText() {
      let data;
      if (this.datas.commodityType === 0) data = "视频带链接";
      if (this.datas.commodityType === 1) data = "视频+产品";
      return data;
    },
  },
  props: {
    datas: Object,
  },
  data() {
    return {
      dragOptions: {
        animation: 200,
      },
      curVideoIndex: '',
      changeIndex: '',
      sectionList: [], // 学段
      subjectList: [], // 科目
      flagTags: [],   // 运营标签
      secondIndex: '',
      changeType: ''
    };
  },
  methods: {
    // 获取学段
    async getFirstLevels() {
      const res = await getFirstLevels({ templateId: "1360833972011046" });
      this.sectionList = res.data;
    },
    changeSection(e) {
      this.getChildrenList(e);
    },
    // 获取某个学段下的科目
    async getChildrenList(e) {
      const res = await getChildrenList({
        levelIds: e.sectionCode.slice(0, 16),
        templateId: "1360833972011046",
      });
      this.subjectList = res.data;
    },
    // 样式一添加内容
    addVideoList (commodityType) {
      if (commodityType === 0) {
        this.datas.imageList.push({
          videoUrl: "",
          vimgUrl: "",
          title: "这里是标题",
          desc: '这里是描述',
          subjectCode: "",
          sectionCode: "",
          flag: "限时体验",
          btnText: "1元领取",
          linkurl: "",
          isVipShow: 2
        });
      }
      if (commodityType === 1) {
        this.datas.imageList.push({
          videoUrl: "",
          vimgUrl: "",
          subjectCode:"",
          flag:"",
          linkurl:"",
          isVipShow:1,
          linktype:1,
          list: []
        })
      }
    },
    // 样式二产品列表添加内容
    addVideoContent (index) {
      this.datas.imageList[index].list.push({
        src: "",
        text: "这里显示标题最多显示2行",
        desc: "这里显示描述",
        type: "1",
        id: "",
        courseNum: 0,
        studyStatus: '',
        flag: '',
        flagTagsArr: [],
        btnText: '立即抢购',
        isVipShow: 1,
        showPrice: 1,
        isShowPrice: true
      })
    },
    // 选择链接
    selectThisLink(resu) {
      if (resu.sectionIndex !== undefined) {
        this.datas.imageList.forEach((ite, ind) => {
          this.datas.imageList[ind].list.forEach((it, inx) => {
            if (ind == resu.sectionIndex) {
              if (inx == resu.index) {
                this.datas.imageList[ind].list[inx].url = resu.url;
                this.datas.imageList[ind].list[inx].linktype = resu.linktype;
                this.datas.imageList[ind].list[inx].id = resu.id;
                this.datas.imageList[ind].list[inx].text = resu.name;
                this.datas.imageList[ind].list[inx].desc = resu.note;
              }
            }
          });
        });
      } else {
        this.datas.imageList.forEach((it, ind) => {
          if (ind == resu.index) {
            it.linkurl = resu.url;
            it.linktype = resu.linktype;
            it.title = resu.name;
            it.desc = resu.note;
            it.id = resu.id;
            it.type = resu.type;
            it.suitesId = resu.suitesId;
            it.goodsLabel = resu.goodsLabel;
          }
        });
      } 
    },
    changeValue (val,index,ind) {
      const valstr = val.join(',');
      this.datas.imageList[index].list[ind].flag = valstr;
    },
    radioChange (val,index,ind) {
      if (val === 1) {
        this.datas.imageList[index].list[ind].isShowPrice = true;
      } else {
        this.datas.imageList[index].list[ind].isShowPrice = false;
      }
    },
    showUpload(type,index,ind) {
      this.changeType = type;
      this.changeIndex = index;
      this.secondIndex = ind;
      this.$refs.upload.showUpload();
    },
    // 提交
    uploadInformation(res) {
      switch (this.changeType) {
        case 'first':
          this.datas.imageList[this.changeIndex].vimgUrl = res;
          break;
        case 'second':
        this.datas.imageList[this.changeIndex].list[this.secondIndex].src = res;
          break;
      }
    },
    clearThisLink (index,ind) {
      if (ind === undefined) {
        this.datas.imageList[index].linkurl = '';
      } else {
        this.datas.imageList[index].list[ind].url = '';
      }
    },
    uploadVideo(index) {
      this.curVideoIndex = index;
      this.$refs.uploadvideo.showUpload();
    },
    uploadVideoSrc(url) {
      this.datas.imageList[this.curVideoIndex].videoUrl = url;
    },
    // 删除视频
    deleteupvideo(index) {
       this.datas.imageList[index].videoUrl = "";
    },
    /* 删除图片 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    },
    deleteListImg (index,ind) {
      this.datas.imageList[index].list.splice(ind, 1);
    }
  },
  created () {
    if (this.datas.commodityType === 0) {
      this.getFirstLevels();
    }
  }
};
</script>
<style lang="scss" scoped>
.videoPromotionstyle {
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  padding: 0 10px;
  box-sizing: border-box;
  .bor {
    height: 1px;
    margin: 20px 0;
    background-color: #ebedf0;
  }
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }

  .lef {
    .el-form-item__label {
      text-align: left;
    }
    p {
      line-height: 4px;
    }
  }
  /* 样式 */
  .vidCommodityType {
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-bottom: 30px;
    span {
      display: inline-block;
      width: 58px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      background: #ebedf0;
      color: #979797;
      border: 1px solid #fff;
      cursor: pointer;
      transition: all 0.5s;
      &:hover {
        border: 1px solid #155bd4;
        color: #155bd4;
      }
      &.active {
        border: 1px solid #155bd4;
        background-color: #e0edff;
        color: #155bd4;
      }
    }
  }

  .imgAndVideo1{
    margin-bottom: 10px;
    span{
      font-size: 14px;
      color: #666666;
      margin-right: 10px;
    }
    .imag{
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      img{
        width: 120px;
        height: 120px;
      }
    }
    .upvideo{
      display: flex;
      align-items: center;
      p{
        position: absolute;
        bottom: 0px;
        left: 0;
        width: 100%;
        height: 20px;
        line-height: 20px;
        color: #fff;
        text-align: center;
        background-color: rgba(255, 255, 255, 0.5);
      }
      p:hover{
        cursor: pointer;
      }
    }
  }
   /* 卡片列表 */
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;

    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }

    /* 图片 */
    .imag {
      width: 90px;
      height: 90px;
      border-radius: 5px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        display: inline-block;
      }
      span {
        background: rgba(0, 0, 0, 0.5);
        font-size: 12px;
        position: absolute;
        left: 0px;
        bottom: 0px;
        display: inline-block;
        width: 100%;
        text-align: center;
        color: #fff;
        height: 20px;
        line-height: 20px;
      }
    }
    .upvideo{
      margin-top: 20px;
      position: relative;
      p {
        position: absolute;
        left: 0;
        bottom: -10px;
        width: 100%;
        height: 20px;
        font-size: 12px;
        color: #fff;
        background: rgba(0, 0, 0, 0.5);
        text-align: center;
        line-height: 20px;
      }
      :hover{
        cursor: pointer;
      }
    }

    /* 图片字 */
    .imgText {
      width: 80%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding-left: 10px;
      justify-content: space-between;
      /* 图片字 */
      .imgTextChild {
        width: 100%;
        display: flex;
        box-sizing: border-box;
        justify-content: space-between;
        .fir-sele.el-select {
          width: 40%;
        }
      }
    }
  }
}
</style>
